//
//  DYWHomeCell.swift
//  sinaWeibo
//
//  Created by 董艳武 on 16/2/20.
//  Copyright © 2016年 董艳武. All rights reserved.
//

import UIKit
import SnapKit
class DYWHomeCell: UITableViewCell {
    
    /// 更改约束的属性
    var changConstraint:Constraint?

    // MARK:--重写父类的构造方法
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        self.backgroundColor = UIColor.whiteColor()
        self.selectionStyle = UITableViewCellSelectionStyle.None
        setUI()
    }

    // MARK:--重写了构造方法之后，从 sb/xib 加载控件时会报错
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // MARK:--模型属性
    var model:DYWWeiboModel? {
        didSet {
            // 为顶部的视图的 model 属性 赋值
            topView.model = model
            // 卸载旧的约束
            self.changConstraint?.uninstall()
            if let retweetModel = model?.retweeted_status {
                // 为转发微博的 model 属性赋值
                retweetWeibo.model = retweetModel
                // 修改底部的约束
                bottomView.snp_updateConstraints(closure: { (make) -> Void in
                    self.changConstraint = make.top.equalTo(retweetWeibo.snp_bottom).offset(commonMargin).constraint
                })
                // 设置转发微博显示
                retweetWeibo.hidden = false
            } else {
                // 转发微博属性为 nil 
                // 修改底部的约束
                bottomView.snp_updateConstraints(closure: { (make) -> Void in
                    self.changConstraint = make.top.equalTo(topView.snp_bottom).offset(commonMargin).constraint
                })
                // 设置转发微博隐藏
                retweetWeibo.hidden = true
            }
        }
    }
    
    // MARK:--重写 layOutSubViews 方法
    override func layoutSubviews() {
        super.layoutSubviews()
//        setUI()
    }

    /// 懒加载顶部的视图
    lazy var topView:DYWTopView = DYWTopView()
    
    /// 懒加载底部的视图
    lazy var bottomView:DYWBottomView = DYWBottomView()
    
    /// 懒加载转发微博属性
    lazy var retweetWeibo:DYWReweetWeiBo = DYWReweetWeiBo()
    
      /// 设置 ui
    private func setUI () {
        contentView.addSubview(topView)
        contentView.addSubview(retweetWeibo)
        contentView.addSubview(bottomView)


        self.contentView.backgroundColor = IndianRed
        
       // FIXME:-- 自定义 cell 中 设置顶部的视图的约束 用 contentView 作为参考，不要用 self 作为参考
        
        topView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(contentView.snp_top).offset(commonMargin)
            make.left.equalTo(contentView.snp_left).offset(commonMargin)
            make.right.equalTo(contentView.snp_right).offset(-commonMargin)
        }
        
        retweetWeibo.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(topView.snp_bottom).offset(commonMargin)
            make.left.equalTo(topView.snp_left)
            make.right.equalTo(topView.snp_right)
        }

        
        bottomView.snp_makeConstraints { (make) -> Void in
            make.height.equalTo(35)
            make.leading.equalTo(topView.snp_leading)
            make.trailing.equalTo(topView.snp_trailing)
            // 用约束属性保存要修改的约束
            self.changConstraint =  make.top.equalTo(retweetWeibo.snp_bottom).offset(4).constraint
        }
        
        contentView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(self.snp_top)
            make.left.equalTo(self.snp_left)
            make.right.equalTo(self.snp_right)
            make.bottom.equalTo(bottomView.snp_bottom).offset(commonMargin)
        }
        
        contentView.layer.cornerRadius = 10
        contentView.layer.masksToBounds = true
        
        topView.layer.cornerRadius = 10
        topView.layer.masksToBounds = true
        
        bottomView.layer.cornerRadius = 7
        bottomView.layer.masksToBounds = true
        bottomView.layer.borderColor = IndianRed.CGColor
        bottomView.layer.borderWidth = 2


    }
    
}
